import { message } from "@/utils/message";
import { queryChatGoodsList } from "@/api/syschat";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, onMounted } from "vue";

export function useGoods() {
  const INITIAL_DATA = {
    type: "",
    sum: ""
  };

  const searchForm = reactive({
    goods: "",
    type: "",
    used: ""
  });

  const authDialogVisible = ref(false);
  const authFormData = ref({ ...INITIAL_DATA });

  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });

  const columns: TableColumnList = [
    {
      type: "selection",
      width: 55,
      align: "left",
      hide: ({ checkList }) => !checkList.includes("勾选列")
    },
    {
      label: "序号",
      type: "index",
      width: 70,
      hide: ({ checkList }) => !checkList.includes("序号列")
    },
    {
      label: "编号",
      prop: "id",
      minWidth: 130
    },
    {
      label: "授权码",
      prop: "goods",
      minWidth: 130
    },
    {
      label: "类型",
      prop: "type",
      minWidth: 90,
      formatter: row => {
        switch (row.type) {
          case 1:
            return "50次";
          case 2:
            return "300次";
          case 3:
            return "1500次";
          case 4:
            return "赠送20次";
        }
      }
    },
    {
      label: "状态",
      prop: "used",
      minWidth: 90,
      formatter: row => {
        switch (row.used) {
          case 0:
            return "未使用";
          case 1:
            return "已使用";
        }
      }
    },
    {
      label: "创建时间",
      minWidth: 90,
      prop: "dataTime"
    },
    {
      label: "使用时间",
      minWidth: 90,
      prop: "usedTime"
    }
  ];

  function handleAdd() {
    authDialogVisible.value = true;
    authFormData.value = { ...INITIAL_DATA };
  }

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
    onSearch();
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }

  async function onSearch() {
    loading.value = true;
    queryChatGoodsList({
      goods: searchForm.goods,
      type: searchForm.type,
      used: searchForm.used,
      pageSize: pagination.pageSize,
      currentPage: pagination.currentPage
    })
      .then(res => {
        if (res.success) {
          dataList.value = res.data.list;
          pagination.total = res.data.total;
          loading.value = false;
        } else {
          message("查询数据异常:" + res.errMsg, { type: "error" });
        }
      })
      .catch(error => {
        message("查询数据异常:" + JSON.stringify(error), { type: "error" });
      });
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };

  onMounted(() => {
    onSearch();
  });

  return {
    authDialogVisible,
    searchForm,
    loading,
    columns,
    dataList,
    pagination,
    handleAdd,
    onSearch,
    resetForm,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange
  };
}
